import { RequestUtil } from '@/util/ele_server_req';
export const CameraBrandsUtil = {
	//查询全部的摄像头品牌
	LoadAllCameraBrands_InCommon: async allCameraBrandArray => {
		allCameraBrandArray.splice(0, allCameraBrandArray.length);
		let searchCondition = {
			dictType: 'camera_brand'
		};
		await RequestUtil.do_get_async(
			'system/dict/data/listAll',
			searchCondition,
			response => {
				let count = response.data.total;
				if (count > 0) {
					Object.assign(allCameraBrandArray, response.data.rows);
				}
			},
			error => {
				console.log(error);
			}
		);
	},
	QueryCameraBrand_InCommon: (allCameraBrandArray, component_in_ref, queryString: string, cb: (arg: any) => void) => {
		component_in_ref.splice(0, component_in_ref.length);
		//component_in_ref = [];
		if (queryString) {
			queryString = queryString.trim();
			for (let one of allCameraBrandArray) {
				if (one.dictLabel.includes(queryString)) {
					component_in_ref.push(one);
				}
			}
		} else {
			let len = allCameraBrandArray.length;
			if (len > 10) {
				len = 10;
			}
			Object.assign(component_in_ref, allCameraBrandArray.slice(0, len));
		}
		cb(component_in_ref);
	},
	//选择某个品牌后要更新
	SelectCameraBrand_InCommon: (component_in_ref, item: any) => {
		if (item.dictValue != '' && item.dictValue > 0) {
			component_in_ref.brandId = item.dictValue;
			component_in_ref.brandName = item.dictLabel;
		} else {
			component_in_ref.brandId = '';
			component_in_ref.brandName = '';
		}
	},
	//从全部的品牌中定位匹配的
	FindOneCameraBrand_InCommon: (id, allCameraBrandArray) => {
		let target = { brandId: '', brandName: '' };
		for (let one of allCameraBrandArray) {
			if (one.dictValue == id) {
				target.brandId = id;
				target.brandName = one.dictLabel;
				break;
			}
		}
		return target;
	}
};

//查询小区的工具
export const CommunityUtil = {
	QueryFirstLevelRegion_InCommon: regionArray => {
		regionArray.splice(0, regionArray.length);
		//初始化下拉菜单（地区）
		RequestUtil.do_get(
			`/elevator/region/list`,
			{ level: 1 },
			response => {
				Object.assign(regionArray, response.data.rows);
			},
			error => {
				console.log(error);
			}
		);
	},
	//函数cb，是专门用于返回查询结果的函数。不需要自定义
	QueryCommunity_InCommon: (communityArray, component_in_ref, queryString: string, cb: (arg: any) => void) => {
		communityArray.splice(0, communityArray.length);
		let searchCondition = {
			pageNum: 1,
			pageSize: 10
		};
		if (component_in_ref.selectedDistrict) {
			searchCondition['street'] = component_in_ref.selectedDistrict;
		} else if (component_in_ref.selectedDepartment) {
			searchCondition['district'] = component_in_ref.selectedDepartment;
		} else if (component_in_ref.selectedCity) {
			searchCondition['city'] = component_in_ref.selectedCity;
		} else if (component_in_ref.selectedProvince) {
			searchCondition['province'] = component_in_ref.selectedProvince;
		} else if (queryString) {
			searchCondition['name'] = queryString;
		}
		RequestUtil.do_get_async(
			'/elevator/apartment/list',
			searchCondition,
			response => {
				let count = response.data.total;
				if (count < 1) {
					Object.assign(communityArray, [{ name: '', provinceName: '抱歉，没找到小区', apartmentId: 0 }]);
				} else {
					Object.assign(communityArray, response.data.rows);
				}
			},
			error => {
				console.log(error);
			}
		);
		cb(communityArray);
	},
	//选择某个小区后要更新formInline.community
	SelectCommunity_InCommon: (component_in_ref, item: any) => {
		if (item.apartmentId == 0) {
			component_in_ref.apartmentId = '';
			component_in_ref.community = '';
		} else {
			component_in_ref.apartmentId = item.apartmentId; //记录选中的小区ID
			component_in_ref.community = item.name;
		}
	},
	changeProvince: async (component_in_ref, val) => {
		component_in_ref.selectedCity = '';
		RequestUtil.do_get(
			'/elevator/region/list',
			{ pid: val },
			response => {
				Object.assign(component_in_ref.city, response.data.rows);
			},
			error => {
				console.log(error);
			}
		);
	},
	changeCity: async (component_in_ref, val) => {
		component_in_ref.selectedDepartment = '';
		RequestUtil.do_get(
			'/elevator/region/list',
			{ pid: val },
			response => {
				Object.assign(component_in_ref.department, response.data.rows);
			},
			error => {
				console.log(error);
			}
		);
	},
	changeDepartment: async (component_in_ref, val) => {
		component_in_ref.selectedDistrict = '';
		RequestUtil.do_get(
			'/elevator/region/list',
			{ pid: val },
			response => {
				Object.assign(component_in_ref.district, response.data.rows);
			},
			error => {
				console.log(error);
			}
		);
	}
};
